/**
 * Project Name:gc.sms.mvn
 * File Name:RoleServiceImpl.java
 * Package Name:com.clps.dev.sms.service.impl
 * Date:2018年11月15日下午11:00:06
 * Copyright (c) 2018, longbiosphere@163.com All Rights Reserved.
 *
 */
package com.clps.dev.sms.service.sm.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clps.dev.sms.common.CommonData;
import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.common.service.impl.BaseServiceImpl;
import com.clps.dev.sms.dao.sm.RoleDao;
import com.clps.dev.sms.entity.sm.Role;
import com.clps.dev.sms.service.sm.IRoleService;
import com.clps.dev.sms.util.result.ResponseResultUtil;

/**
 * ClassName: RoleServiceImpl <br/>
 * Description: date: 2018年11月15日 下午11:00:06 <br/>
 *
 * @author long.ma
 * @version V1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

	/**
	 * Creates a new instance of RoleServiceImpl.
	 *
	 * @param clazz
	 */
	
	public RoleServiceImpl() {
		super(Role.class);
		 
	}

	private Logger logger = Logger.getLogger(MenuServiceImpl.class);
	@Autowired
	private RoleDao roleDao;

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IRoleService#addRole(com.clps.dev.sms.entity.sm.Role)
	 */
	@Override
	public ResponseResult<Role> addRole(Role role) {
		if (role == null) {
			logger.error("role不能为空");
			return ResponseResultUtil.error(ResultType.OBJECT_NULL);
		}
		if (role.getRoleName() == null || role.getRoleCreatedEmployeeId() == null || role.getRoleCreatedTime() == null
				|| role.getRoleUpdatedEmployeeId() == null) {
			logger.error("这个字段不能为空");
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		List<Role> list = roleDao.queryByRoleName(role.getRoleName());
		if (0 != list.size()) {
			logger.error("这个字段必须唯一");
			return ResponseResultUtil.error(ResultType.CONTENT_REPEAT);
		}

		int i = roleDao.save(role);
		if (i > 0) {
			return ResponseResultUtil.success(ResultType.ADD_SUCCESS);
		} else {
			return ResponseResultUtil.error(ResultType.ADD_FAULT);
		}

	}

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IRoleService#editRole(com.clps.dev.sms.entity.sm.Role)
	 */
	@Override
	public ResponseResult<Role> editRole(Role role) {
		if (role == null) {
			logger.error("role不能为空");
			return ResponseResultUtil.error(ResultType.OBJECT_NULL);
		}
		if (role.getRoleName() == null || role.getRoleCreatedEmployeeId() == null || role.getRoleCreatedTime() == null
				|| role.getRoleUpdatedEmployeeId() == null) {
			logger.error("这个字段不能为空");
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		if (!roleDao.queryById(role.getRoleId()).getRoleName().equals(role.getRoleName())) {
			List<Role> list = roleDao.queryByRoleName(role.getRoleName());
			if (0 != list.size()) {
				logger.error("这个字段必须唯一");
				return ResponseResultUtil.error(ResultType.CONTENT_REPEAT);
			}
		}
		Role roleQuery = roleDao.queryById(role.getRoleId());
		if (!roleQuery.getRoleCreatedEmployeeId().equals(role.getRoleCreatedEmployeeId())
				|| !roleQuery.getRoleCreatedTime().equals(role.getRoleCreatedTime())) {
			logger.error("这个字段不能修改");
			return ResponseResultUtil.error(ResultType.CANNOT_CHANGE);
		}
		int i = roleDao.update(role, role.getRoleId());
		if (i > 0) {
			return ResponseResultUtil.success(ResultType.UPDATE_SUCCESS);
		} else {
			return ResponseResultUtil.error(ResultType.UPDATE_FAULT);
		}

	}

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IRoleService#queryRole(java.lang.Integer)
	 */
	@Override
	public ResponseResult<Role> queryRole(Integer id) {
		Role role = roleDao.queryById(id);
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,role);
	}

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IRoleService#queryAllRole()
	 */
	@Override
	public ResponseResult<Role> queryAllRole() {
		List<Role> list = roleDao.queryALl();
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,list);
	}

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IRoleService#deleteRole(com.clps.dev.sms.entity.sm.Role)
	 */
	@Override
	public ResponseResult<Role> deleteRole(Role role) {
		int i=roleDao.deleteById(role.getRoleId());
		if(i==CommonData.success) {
			return ResponseResultUtil.success(ResultType.DELETE_SUCCESS);
		}else {
			return ResponseResultUtil.error(ResultType.DELETE_FAULT);
		}
		
	}

	
}
